package com.wxw.gulidemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxw.gulidemo.R;
import com.wxw.gulidemo.entity.EduCourse;
import com.wxw.gulidemo.entity.vo.CourseForm;
import com.wxw.gulidemo.entity.vo.CoursePublishVo;
import com.wxw.gulidemo.entity.vo.CourseQuery;
import com.wxw.gulidemo.entity.vo.frontVo.CourseWebVo;
import com.wxw.gulidemo.service.IEduCourseService;
import com.wxw.gulidemo.vo.EduCourseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xinxinzi
 * @since 2022-06-01
 */
@RestController
@RequestMapping("/eduService/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private IEduCourseService eduCourseService;

    //添加课程基本信息
    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseForm courseForm){
        String id = eduCourseService.addCourseInfo(courseForm);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息(目的: 信息回显,为修改课程信息做准备)
    @GetMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CourseForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm",courseInfoForm);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseForm courseInfoForm){
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getpublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse",publishCourseInfo);
    }

    //查询所有课程
    @RequestMapping("/getCourseList")
    public R getCourseList(){
        return R.ok().data("list",eduCourseService.list(null));
    }

    //带条件分页查询课程
    @ApiOperation(value = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R  pageCourseCondition(@PathVariable Long page,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> coursePage = new Page<>(page,limit);
        eduCourseService.pageQuery(coursePage,courseQuery);
        List<EduCourse> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    //课程列表中删除课程方法
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCourseById(@PathVariable String id){
        boolean flag = eduCourseService.removeCourse(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal"); //设置课程发布状态
        eduCourse.setIsDeleted(0);
        eduCourse.setId(id);
        boolean flag = eduCourseService.updateById(eduCourse);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseId){
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseId);

        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo,eduCourseVo);

        return eduCourseVo;
    }
}
